package wymiana.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.annotations.Type;

import com.ibm.icu.text.SimpleDateFormat;

@Entity
@Table(name = "mail")
public class Mail extends BaseEntity {

	/**
	 * 
	 */
	public Mail() {
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 7372922254207239062L;
	public final static int USERNAME_LENGTH = 64;
	public final static int CONTENT_LENGTH = 4000;
	public final static int TITLE_LENGTH = 1000;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(updatable = false)
	private Long id;

	@ManyToOne
	@JoinColumn(name = "sender", nullable = false)
	@OnDelete(action = OnDeleteAction.CASCADE)
	private User sender;

	@ManyToOne
	@JoinColumn(name = "recipient", nullable = false)
	@OnDelete(action = OnDeleteAction.CASCADE)
	private User recipient;

	@Column(length = CONTENT_LENGTH)
	private String content;

	@Column(length = TITLE_LENGTH)
	private String title;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false, length = 7)
	private Date date;

	@Type(type = "org.hibernate.type.NumericBooleanType")
	@Column(nullable = false)
	private Boolean readed;

	@Type(type = "org.hibernate.type.NumericBooleanType")
	@Column(nullable = false)
	private Boolean trash;

	@Type(type = "org.hibernate.type.NumericBooleanType")
	@Column(nullable = false)
	private Boolean trash_sender;
	
	@Type(type = "org.hibernate.type.NumericBooleanType")
	@Column(nullable = false)
	private Boolean deleted;

	@Type(type = "org.hibernate.type.NumericBooleanType")
	@Column(nullable = false)
	private Boolean deleted_sender;

	/**
	 * @return the trash_sender
	 */
	public Boolean getTrash_sender() {
		return trash_sender;
	}

	/**
	 * @param trashSender
	 *            the trash_sender to set
	 */
	public void setTrash_sender(Boolean trashSender) {
		trash_sender = trashSender;
	}

	/**
	 * @return the deleted
	 */
	public Boolean getDeleted() {
		return deleted;
	}

	/**
	 * @param deleted
	 *            the deleted to set
	 */
	public void setDeleted(Boolean deleted) {
		this.deleted = deleted;
	}

	/**
	 * @return the deleted_sender
	 */
	public Boolean getDeleted_sender() {
		return deleted_sender;
	}

	/**
	 * @param deletedSender
	 *            the deleted_sender to set
	 */
	public void setDeleted_sender(Boolean deletedSender) {
		deleted_sender = deletedSender;
	}

	/**
	 * @return the trash
	 */
	public Boolean getTrash() {
		return trash;
	}

	/**
	 * @param trash
	 *            the trash to set
	 */
	public void setTrash(Boolean trash) {
		this.trash = trash;
	}

	/**
	 * @param id
	 * @param sender
	 * @param recipient
	 * @param content
	 * @param title
	 * @param date
	 * @param readed
	 * @param trash
	 * @param deleted
	 */
	public Mail(Long id, User sender, User recipient, String content,
			String title, Date date, Boolean readed, Boolean trash) {
		this.id = id;
		this.sender = sender;
		this.recipient = recipient;
		this.content = content;
		this.title = title;
		this.date = date;
		this.readed = readed;
		this.trash = trash;
	}

	/**
	 * @param sender
	 * @param recipient
	 * @param content
	 * @param title
	 */
	public Mail(User sender, User recipient, String content, String title) {
		this.sender = sender;
		this.recipient = recipient;
		this.content = content;
		this.title = title;
		this.date = new Date();
		this.readed = false;
		this.trash = false;
		this.trash_sender = false;
		this.deleted = false;
		this.deleted_sender = false;
	}

	/**
	 * @param id
	 * @param sender
	 * @param recipient
	 * @param content
	 * @param title
	 * @param date
	 * @param readed
	 * @param trash
	 * @param trashSender
	 * @param deleted
	 * @param deletedSender
	 */
	public Mail(Long id, User sender, User recipient, String content,
			String title, Date date, Boolean readed, Boolean trash,
			Boolean trashSender, Boolean deleted, Boolean deletedSender) {
		this.id = id;
		this.sender = sender;
		this.recipient = recipient;
		this.content = content;
		this.title = title;
		this.date = date;
		this.readed = readed;
		this.trash = trash;
		trash_sender = trashSender;
		this.deleted = deleted;
		deleted_sender = deletedSender;
	}

	/**
	 * @return the read
	 */
	public Boolean getReaded() {
		return readed;
	}

	/**
	 * @param read
	 *            the read to set
	 */
	public void setReaded(Boolean readed) {
		this.readed = readed;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public User getSender() {
		return sender;
	}

	public void setSender(User sender) {
		this.sender = sender;
	}

	public User getRecipient() {
		return recipient;
	}

	public void setRecipient(User recipient) {
		this.recipient = recipient;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String cutString(String text) {
		if (text.length() > 80)
			return text.substring(0, 80) + "...";
		else
			return text;
	}

	public String getFullDate() {
		String DATE_FORMAT = "dd.MM.yyyy HH:mm";

		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		if (date != null)
			return sdf.format(date);
		else
			return "";
	}
}
